<template>
  <el-dialog
    title="条件定义新增"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose"
  >
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="表名">
        <el-select
          @change="tableNameChangChildren"
          style="width: 430px"
          v-model="form.tabName"
          placeholder="请选择"
        >
          <el-option
            v-for="item in nameOption"
            :key="item.tableName"
            :label="item.tableShowName"
            :value="item.tableName"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="字段">
        <el-select
          style="width: 430px"
          v-model="form.tagField"
          placeholder="请选择"
        >
          <el-option
            v-for="item in fieldOptions"
            :key="item.fieldName"
            :label="item.fieldShowName"
            :value="item.fieldName"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="比较符">
        <el-select
          style="width: 430px"
          v-model="form.tagOperator"
          placeholder="请选择"
        >
          <el-option
            v-for="item in comparisonOptions"
            :key="item.CODE"
            :label="item.NAME"
            :value="item.CODE"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>
  
  <script>
import {
  queryDict,
  tagTableAndColumnConfigList,
} from "@/api/students/organization/consult";
import store from "@/store";
export default {
  props: {
    list: Array,
  },
  data() {
    return {
      dialogVisible: false,
      form: {
        tabName: "",
        tagField: "",
        tagOperator: "",
        tagSort:this.list.length
      },
      nameOption: [],
      fieldOptions: [],
      comparisonOptions: [],
    };
  },
  methods: {
    init() {
      // this.list.push(this.form);
      // console.log(this.list,"list");
      // this.dialogVisible = true;
      // this.tableNameList();
      // // this.fieldList();
      // this.queryDict();
      this.clear();
    },
    tableNameChangChildren() {
      this.fieldList(this.form.tabName); // 调用fieldList方法并传入选中的表名
    },
    // 表名
    tableNameList() {
      const params = {
        userId: store.getters.user_id.toString(),
        userName: store.getters.userName.toString(),
        token: store.getters.token.toString(),
        type: "1",
      };
      tagTableAndColumnConfigList(params).then((res) => {
        this.nameOption = res.resultdata;
      });
    },
    // 字段
    fieldList(tabName) {
      const params = {
        userId: store.getters.user_id.toString(),
        userName: store.getters.userName.toString(),
        token: store.getters.token.toString(),
        type: "2",
        tableName: tabName,
      };
      tagTableAndColumnConfigList(params).then((res) => {
        this.fieldOptions = res.resultdata;
      });
    },
    // 比较符
    queryDict() {
      const params = {
        userId: store.getters.user_id.toString(),
        userName: store.getters.userName.toString(),
        token: store.getters.token.toString(),
      };
      queryDict(params).then((res) => {
        this.comparisonOptions = res.resultdata;
      });
    },
    submit() {
      this.list.push(this.form);
      this.dialogVisible = false;
    },
    handleClose() {
      this.$emit("addData", this.form);
      // this.dialogVisible = false;
    },
    clear() {
      this.form.tabName = "";
      this.form.tagField = "";
      this.form.tagOperator = "";
    },
    
  },
};
</script>
<style scoped>
::v-deep .el-dialog {
  margin-top: 15vh !important;
}
</style>